﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using Microsoft.Telco.Controls.Properties;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Microsoft.Telco.Controls
{
    /// <summary>
    /// Control that enables user to send SMS/MMS
    /// </summary>
    #region Parts and States Model Declarations
    //Common parts
    [TemplatePart(Name = "HeaderPanel", Type = typeof(StackPanel))]
    [TemplatePart(Name = "HeaderTextBlock", Type = typeof(TextBlock))]
    [TemplatePart(Name = "PhoneNumberControl", Type = typeof(PhoneNumberControl))]
    [TemplatePart(Name = "MessageTextbox", Type = typeof(TextBox))]
    [TemplatePart(Name = "MessageTextblock", Type = typeof(TextBlock))]
    [TemplatePart(Name = "SendButton", Type = typeof(Button))]
    [TemplatePart(Name = "MessageDeliveryStatusBorder", Type = typeof(Border))]
    [TemplatePart(Name = "MessageDeliveryStatusTextblock", Type = typeof(TextBlock))]
    [TemplatePart(Name = "MessageContainerBorder", Type = typeof(Border))]

    //SMS parts
    [TemplatePart(Name = "ListReceivedMessages", Type = typeof(ReceivedMessagesList))]

    //MMS parts
    [TemplatePart(Name = "AttachFilesImage", Type = typeof(Image))]
    [TemplatePart(Name = "ListFileAttachments", Type = typeof(AttachedFilesList))]
    [TemplatePart(Name = "FilesAttachedPanel", Type = typeof(StackPanel))]

    //Visual State Groups
    [TemplateVisualState(Name = "Success", GroupName = "MessageSendStateGroup")]
    [TemplateVisualState(Name = "Failure", GroupName = "MessageSendStateGroup")]
    #endregion
    public class SMSControl : TelcoControlBase
    {
        #region events
        /// <summary>
        /// Occurs when send SMS is triggered.
        /// </summary>
        public event EventHandler SendSMSTriggerEvent;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="SMSControl"/> class.
        /// </summary>
        public SMSControl()
        {
            //Common settings
            this.DefaultStyleKey = typeof(SMSControl);
            ShowHeader = true;
            this.MouseLeftButtonDown += delegate
            {
                ControlHelper.HideAllPopups();
            };

            //SMS settings
           

            //MMS settings
            //this.AllowMultipleFilesInMMS = true;
            //this.FileFilters = Properties.Resources.MMS_FileFilters;
            //Files = new ObservableCollection<FileInfo>();
            //Files.CollectionChanged += delegate
            //{
            //    if (Files.Count > 0)
            //    {
            //        FilesAttachedPanel.Visibility = Visibility.Visible;
            //    }
            //    else
            //    {
            //        FilesAttachedPanel.Visibility = Visibility.Collapsed;
            //    }
            //};
        }

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) call <see cref="M:System.Windows.Controls.Control.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            //Common parts
            HeaderPanel = GetTemplateChild("HeaderPanel") as StackPanel;
            HeaderTextBlock = GetTemplateChild("HeaderTextBlock") as TextBlock;
            SendButton = GetTemplateChild("SendButton") as Button;
            MessageTextbox = GetTemplateChild("MessageTextbox") as TextBox;
            PhoneNumberControl = GetTemplateChild("PhoneNumberControl") as PhoneNumberControl;
            MessageDeliveryStatusBorder = GetTemplateChild("MessageDeliveryStatusBorder") as Border;
            MessageDeliveryStatusTextblock = GetTemplateChild("MessageDeliveryStatusTextblock") as TextBlock;

            if (HeaderPanel != null)
            {
                if (!ShowHeader)
                {
                    HeaderPanel.Visibility = Visibility.Collapsed;
                }
            }

            if (SendButton != null)
            {
                SendButton.Click += SendButton_Click;
            }

            if (MessageTextbox != null)
            {
                MessageTextbox.GotFocus += MessageTextbox_GotFocus;
                MessageTextbox.LostFocus += MessageTextbox_LostFocus;
                MessageTextbox.Text = DefaultMessageText;
            }

            //SMS parts
            ListReceivedMessages = GetTemplateChild("ListReceivedMessages") as ReceivedMessagesList;

            //MMS parts
            //AttachFilesImage = GetTemplateChild("AttachFilesImage") as Image;
            //ListFileAttachments = GetTemplateChild("ListFileAttachments") as AttachedFilesList;
            //FilesAttachedPanel = GetTemplateChild("FilesAttachedPanel") as StackPanel;

            //if (AttachFilesImage != null)
            //{
            //    AttachFilesImage.Source = ControlHelper.GetImageFromResource("Assets/camera32.png");
            //    AttachFilesImage.MouseLeftButtonDown += AttachFilesImage_MouseLeftButtonDown;
            //}

            //if (ListFileAttachments != null)
            //{
            //    ListFileAttachments.DataContext = Files;
            //}
        }

        #region parts

        ////////////////////////////////////////////////Common parts/////////////////////////////////
        /// <summary>
        /// Gets or sets the header panel.
        /// </summary>
        /// <value>The header panel.</value>
        public StackPanel HeaderPanel { get; set; }

        /// <summary>
        /// Gets or sets the header text block.
        /// </summary>
        /// <value>The header text block.</value>
        public TextBlock HeaderTextBlock { get; set; }

        /// <summary>
        /// Gets or sets the message textbox.
        /// </summary>
        /// <value>The message textbox.</value>
        public TextBox MessageTextbox
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the send button.
        /// </summary>
        /// <value>The send button.</value>
        public Button SendButton
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the phone number control.
        /// </summary>
        /// <value>The phone number control.</value>
        public PhoneNumberControl PhoneNumberControl
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the message delivery status border.
        /// </summary>
        /// <value>The message delivery status border.</value>
        public Border MessageDeliveryStatusBorder
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the message delivery status textblock.
        /// </summary>
        /// <value>The message delivery status textblock.</value>
        public TextBlock MessageDeliveryStatusTextblock { get; set; }

        /// <summary>
        /// Gets or sets the message container border.
        /// </summary>
        /// <value>The message container border.</value>
        public Border MessageContainerBorder { get; set; }

        ////////////////////////////////////////////////SMS parts/////////////////////////////////

        /// <summary>
        /// List holding received messages.
        /// </summary>
        /// <value>The list received messages.</value>
        public ReceivedMessagesList ListReceivedMessages { get; set; }

        ////////////////////////////////////////////////MMS parts//////////////////////////////////

        //// <summary>
        //// Gets or sets the select files button.
        //// </summary>
        //// <value>The select files button.</value>
        //public Image AttachFilesImage { get; set; }

        ///// <summary>
        ///// Gets or sets the file attachments list.
        ///// </summary>
        ///// <value>The file attachments list.</value>
        //public AttachedFilesList ListFileAttachments { get; set; }

        ///// <summary>
        ///// Gets or sets the files attached panel - container for attached files list.
        ///// </summary>
        ///// <value>The files attached panel.</value>
        //public StackPanel FilesAttachedPanel { get; set; }
        #endregion

        #region properties
        /// <summary>
        /// Decides whether to show header text or not.
        /// </summary>
        public bool ShowHeader { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        /// <value>The message.</value>
        public string Message { get { return MessageTextbox.Text; } private set { } }

        /// <summary>
        /// Gets or sets the numbers to send message to.
        /// </summary>
        /// <value>To numbers.</value>
        public string[] ToNumbers { get { return new string[] { this.PhoneNumberControl.PhoneNumber }; } private set { } }

        /// <summary>
        /// Gets or sets the state of the SMS.
        /// </summary>
        /// <value>The state of the SMS.</value>
        public MessageState SMSState { get; set; }

        /// <summary>
        /// Gets or sets the mode (SMS/MMS).
        /// </summary>
        /// <value>The mode.</value>
        public SMSControlMode Mode
        {
            get
            {
                //return SMS always -- MMS out of scope presently
                return SMSControlMode.SMS;  //Files.Count > 0 ? SMSControlMode.MMS : SMSControlMode.SMS;
            }
            private set { }
        }

        //property backing variable
        private ObservableCollection<ReceivedMessage> receivedMessages;

        /// <summary>
        /// Gets or sets the received messages.
        /// </summary>
        /// <value>The received messages.</value>
        public ObservableCollection<ReceivedMessage> ReceivedMessages
        {
            get
            {
                return receivedMessages;
            }
            set
            {
                receivedMessages = value;
                if (ListReceivedMessages != null)
                {
                    ListReceivedMessages.DataContext = receivedMessages;
                }
                receivedMessages.CollectionChanged += delegate
                {
                    SetMessageListVisibility();
                };
                SetMessageListVisibility();
            }
        }      

        ///////////////////////////////////////////////////MMS properties/////////////////////////////////////////
        ///// <summary>
        ///// Gets or sets a value indicating whether [ignore default file open]. If set to true, the developer will have to hook up the 
        ///// eventhandler for FilesSelectedEvent and will have to provide implementation for selecting files to send as MMS
        ///// </summary>
        ///// <value>
        ///// 	<c>true</c> if [default file dialog opening and selection of files is to be ignored]; otherwise, <c>false</c>.
        ///// </value>
        //public bool IgnoreDefaultFileOpen { get; set; }

        ///// <summary>
        ///// Gets or sets the filters for MMS files.
        ///// e.g. 'All files (*.*)|*.*|PNG Images (*.png)|*.png'
        ///// </summary>
        ///// <value>The file filters.</value>
        //public string FileFilters { get; set; }

        ///// <summary>
        ///// Gets or sets a value indicating whether [allow multiple files in MMS].
        ///// </summary>
        ///// <value>
        ///// 	<c>true</c> if [allow multiple files in MMS]; otherwise, <c>false</c>.
        ///// </value>
        //public bool AllowMultipleFilesInMMS { get; set; }

        ///// <summary>
        ///// Gets or sets the MMS files.
        ///// </summary>
        ///// <value>The files selected by user to send as MMS</value>
        //public ObservableCollection<FileInfo> Files { get; set; }
        #endregion

        #region dependency properties
        /// <summary>
        /// Gets or sets the HeaderText for SMSControl.
        /// </summary>
        /// <value>The HeaderText.</value>
        public string HeaderText
        {
            get { return (string)GetValue(HeaderTextProperty); }
            set { SetValue(HeaderTextProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for HeaderText for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty HeaderTextProperty = 
            DependencyProperty.Register("HeaderText", typeof(string), typeof(SMSControl), 
            new PropertyMetadata(ControlResources.SMSControl_DefaultHeaderText, 
                        new PropertyChangedCallback((obj,args)=>{
                            SMSControl control = (SMSControl)obj;
                            if (control.HeaderTextBlock != null)
                            {
                                control.HeaderTextBlock.Text = args.NewValue as string;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the HeaderTextFontFamily for SMSControl.
        /// </summary>
        /// <value>The HeaderTextFontFamily.</value>
        public FontFamily HeaderTextFontFamily
        {
            get { return (FontFamily)GetValue(HeaderTextFontFamilyProperty); }
            set { SetValue(HeaderTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for HeaderTextFontFamily for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty HeaderTextFontFamilyProperty = 
            DependencyProperty.Register("HeaderTextFontFamily", typeof(FontFamily), typeof(SMSControl), 
            new PropertyMetadata(new FontFamily("Arial"), 
                        new PropertyChangedCallback((obj,args)=>{
                            SMSControl control = (SMSControl)obj;
                            if (control.HeaderTextBlock != null)
                            {
                                control.HeaderTextBlock.FontFamily = args.NewValue as FontFamily;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the HeaderTextFontSize for SMSControl.
        /// </summary>
        /// <value>The HeaderTextFontSize.</value>
        public double HeaderTextFontSize
        {
            get { return (double)GetValue(HeaderTextFontSizeProperty); }
            set { SetValue(HeaderTextFontSizeProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for HeaderTextFontSize for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty HeaderTextFontSizeProperty = 
            DependencyProperty.Register("HeaderTextFontSize", typeof(double), typeof(SMSControl), 
            new PropertyMetadata(12.0D, 
                        new PropertyChangedCallback((obj,args)=>{
                            SMSControl control = (SMSControl)obj;
                            if (control.HeaderTextBlock != null)
                            {
                                control.HeaderTextBlock.FontSize = (double)args.NewValue;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the HeaderTextForeground for SMSControl.
        /// </summary>
        /// <value>The HeaderTextForeground.</value>
        public Brush HeaderTextForeground
        {
            get { return (Brush)GetValue(HeaderTextForegroundProperty); }
            set { SetValue(HeaderTextForegroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for HeaderTextForeground for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty HeaderTextForegroundProperty = 
            DependencyProperty.Register("HeaderTextForeground", typeof(Brush), typeof(SMSControl), 
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF,0x66,0x66,0x66)), 
                        new PropertyChangedCallback((obj,args)=>{
                            SMSControl control = (SMSControl)obj;
                            if (control.HeaderTextBlock != null)
                            {
                                control.HeaderTextBlock.Foreground = args.NewValue as Brush;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the HeaderTextFontWeight for SMSControl.
        /// </summary>
        /// <value>The HeaderTextFontWeight.</value>
        public FontWeight HeaderTextFontWeight
        {
            get { return (FontWeight)GetValue(HeaderTextFontWeightProperty); }
            set { SetValue(HeaderTextFontWeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for HeaderTextFontWeight for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty HeaderTextFontWeightProperty = 
            DependencyProperty.Register("HeaderTextFontWeight", typeof(FontWeight), typeof(SMSControl), 
            new PropertyMetadata(FontWeights.Bold, 
                        new PropertyChangedCallback((obj,args)=>{
                            SMSControl control = (SMSControl)obj;
                            if (control.HeaderTextBlock != null)
                            {
                                control.HeaderTextBlock.FontWeight = (FontWeight)args.NewValue;
                            }
                })));

        /// <summary>
        /// Gets or sets the DefaultMessageText for SMSControl.
        /// </summary>
        /// <value>The DefaultMessageText.</value>
        public String DefaultMessageText
        {
            get { return (String)GetValue(DefaultMessageTextProperty); }
            set { SetValue(DefaultMessageTextProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for DefaultMessageText for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty DefaultMessageTextProperty =
            DependencyProperty.Register("DefaultMessageText", typeof(String), typeof(SMSControl),
            new PropertyMetadata(ControlResources.DefaultMessageText,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageTextbox != null)
                            {
                                control.MessageTextbox.Text = args.NewValue as String;
                            }
                        })));

        /// <summary>
        /// Gets or sets the SMS message textbox background.
        /// </summary>
        /// <value>The message textbox background.</value>
        public Brush MessageTextboxBackground
        {
            get { return (Brush)GetValue(MessageTextboxBackgroundProperty); }
            set { SetValue(MessageTextboxBackgroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for Background for SMS message textbox.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageTextboxBackgroundProperty =
            DependencyProperty.Register("MessageTextboxBackground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Colors.DarkGray),
                new PropertyChangedCallback((obj, args) =>
                {
                    SMSControl control = (SMSControl)obj;
                    if (control.MessageTextbox != null)
                    {
                        control.MessageTextbox.Background = args.NewValue as Brush;
                    }
                })));

        /// <summary>
        /// Gets or sets the MessageTextboxFontFamily for SMSControl.
        /// </summary>
        /// <value>The MessageTextboxFontFamily.</value>
        public FontFamily MessageTextboxFontFamily
        {
            get { return (FontFamily)GetValue(MessageTextboxFontFamilyProperty); }
            set { SetValue(MessageTextboxFontFamilyProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageTextboxFontFamily for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageTextboxFontFamilyProperty =
            DependencyProperty.Register("MessageTextboxFontFamily", typeof(FontFamily), typeof(SMSControl),
            new PropertyMetadata(new FontFamily("Arial"),
            new PropertyChangedCallback((obj, args) =>
            {
                SMSControl control = (SMSControl)obj;
                if (control.MessageTextbox != null)
                {
                    control.MessageTextbox.FontFamily = args.NewValue as FontFamily;
                }
            })));

        /// <summary>
        /// Gets or sets the MessageTextboxForeground for SMSControl.
        /// </summary>
        /// <value>The MessageTextboxForeground.</value>
        public Brush MessageTextboxForeground
        {
            get { return (Brush)GetValue(MessageTextboxForegroundProperty); }
            set { SetValue(MessageTextboxForegroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageTextboxForeground for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageTextboxForegroundProperty =
            DependencyProperty.Register("MessageTextboxForeground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Colors.Black),
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageTextbox != null)
                            {
                                control.MessageTextbox.Foreground = args.NewValue as Brush;
                            }
                        })));


        /// <summary>
        /// Gets or sets the MessageTextboxFontWeight for SMSControl.
        /// </summary>
        /// <value>The MessageTextboxFontWeight.</value>
        public FontWeight MessageTextboxFontWeight
        {
            get { return (FontWeight)GetValue(MessageTextboxFontWeightProperty); }
            set { SetValue(MessageTextboxFontWeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageTextboxFontWeight for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageTextboxFontWeightProperty =
            DependencyProperty.Register("MessageTextboxFontWeight", typeof(FontWeight), typeof(SMSControl),
            new PropertyMetadata(FontWeights.Normal,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageTextbox != null)
                            {
                                control.MessageTextbox.FontWeight = (FontWeight)args.NewValue;
                            }
                        })));


        /// <summary>
        /// Gets or sets the MessageTextboxFontSize for SMSControl.
        /// </summary>
        /// <value>The MessageTextboxFontSize.</value>
        public double MessageTextboxFontSize
        {
            get { return (double)GetValue(MessageTextboxFontSizeProperty); }
            set { SetValue(MessageTextboxFontSizeProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageTextboxFontSize for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageTextboxFontSizeProperty =
            DependencyProperty.Register("MessageTextboxFontSize", typeof(double), typeof(SMSControl),
            new PropertyMetadata(13.0D,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageTextbox != null)
                            {
                                control.MessageTextbox.FontSize = (double)args.NewValue;
                            }
                        })));

        /// <summary>
        /// Gets or sets the MessageDeliveryStatusTextFontFamily for SMSControl.
        /// </summary>
        /// <value>The MessageDeliveryStatusTextFontFamily.</value>
        public FontFamily MessageDeliveryStatusTextFontFamily
        {
            get { return (FontFamily)GetValue(MessageDeliveryStatusTextFontFamilyProperty); }
            set { SetValue(MessageDeliveryStatusTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageDeliveryStatusTextFontFamily for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageDeliveryStatusTextFontFamilyProperty =
            DependencyProperty.Register("MessageDeliveryStatusTextFontFamily", typeof(FontFamily), typeof(SMSControl),
            new PropertyMetadata(new FontFamily("Portable User Interface"),
            new PropertyChangedCallback((obj, args) =>
            {
                SMSControl control = (SMSControl)obj;
                if (control.MessageDeliveryStatusTextblock != null)
                {
                    control.MessageDeliveryStatusTextblock.FontFamily = args.NewValue as FontFamily;
                }
            })));

        /// <summary>
        /// Gets or sets the MessageDeliveryStatusTextForeground for SMSControl.
        /// </summary>
        /// <value>The MessageDeliveryStatusTextForeground.</value>
        public Brush MessageDeliveryStatusTextForeground
        {
            get { return (Brush)GetValue(MessageDeliveryStatusTextForegroundProperty); }
            set { SetValue(MessageDeliveryStatusTextForegroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageDeliveryStatusTextForeground for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageDeliveryStatusTextForegroundProperty =
            DependencyProperty.Register("MessageDeliveryStatusTextForeground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Colors.Black),
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageDeliveryStatusTextblock != null)
                            {
                                control.MessageDeliveryStatusTextblock.Foreground = args.NewValue as Brush;
                            }
                        })));


        /// <summary>
        /// Gets or sets the MessageDeliveryStatusTextFontWeight for SMSControl.
        /// </summary>
        /// <value>The MessageDeliveryStatusTextFontWeight.</value>
        public FontWeight MessageDeliveryStatusTextFontWeight
        {
            get { return (FontWeight)GetValue(MessageDeliveryStatusTextFontWeightProperty); }
            set { SetValue(MessageDeliveryStatusTextFontWeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageDeliveryStatusTextFontWeight for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageDeliveryStatusTextFontWeightProperty =
            DependencyProperty.Register("MessageDeliveryStatusTextFontWeight", typeof(FontWeight), typeof(SMSControl),
            new PropertyMetadata(FontWeights.Normal,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageDeliveryStatusTextblock != null)
                            {
                                control.MessageDeliveryStatusTextblock.FontWeight = (FontWeight)args.NewValue;
                            }
                        })));

        /// <summary>
        /// Gets or sets the MessageDeliveryStatusTextFontSize for SMSControl.
        /// </summary>
        /// <value>The MessageDeliveryStatusTextFontSize.</value>
        public double MessageDeliveryStatusTextFontSize
        {
            get { return (double)GetValue(MessageDeliveryStatusTextFontSizeProperty); }
            set { SetValue(MessageDeliveryStatusTextFontSizeProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageDeliveryStatusTextFontSize for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageDeliveryStatusTextFontSizeProperty =
            DependencyProperty.Register("MessageDeliveryStatusTextFontSize", typeof(double), typeof(SMSControl),
            new PropertyMetadata(11.0D,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageDeliveryStatusTextblock != null)
                            {
                                control.MessageDeliveryStatusTextblock.FontSize = (double)args.NewValue;
                            }
                        })));

        /// <summary>
        /// Gets or sets the SuccessBorderBackground for SMSControl.
        /// </summary>
        /// <value>The SuccessBorderBackground.</value>
        public Brush SuccessBorderBackground
        {
            get { return (Brush)GetValue(SuccessBorderBackgroundProperty); }
            set { SetValue(SuccessBorderBackgroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for SuccessBorderBackground for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty SuccessBorderBackgroundProperty =
            DependencyProperty.Register("SuccessBorderBackground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Colors.Green),
                    new PropertyChangedCallback((obj, args) =>
                    {
                        SMSControl control = (SMSControl)obj;
                        if (control.MessageDeliveryStatusBorder != null)
                        {
                            control.MessageDeliveryStatusBorder.Background = args.NewValue as Brush;
                        }
                    })));

        /// <summary>
        /// Gets or sets the FailureBorderBackground for SMSControl.
        /// </summary>
        /// <value>The FailureBorderBackground.</value>
        public Brush FailureBorderBackground
        {
            get { return (Brush)GetValue(FailureBorderBackgroundProperty); }
            set { SetValue(FailureBorderBackgroundProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for FailureBorderBackground for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty FailureBorderBackgroundProperty =
            DependencyProperty.Register("FailureBorderBackground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Colors.Red),
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageDeliveryStatusBorder != null)
                            {
                                control.MessageDeliveryStatusBorder.Background = args.NewValue as Brush;
                            }
                        })));

        /// <summary>
        /// Gets or sets the MessageAreaHeight for SMSControl.
        /// </summary>
        /// <value>The height of message container - a border containing message textbox.</value>
        public double MessageAreaHeight
        {
            get { return (double)GetValue(MessageAreaHeightProperty); }
            set { SetValue(MessageAreaHeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessageAreaHeight for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessageAreaHeightProperty =
            DependencyProperty.Register("MessageAreaHeight", typeof(double), typeof(SMSControl),
            new PropertyMetadata(70.0D,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.MessageContainerBorder != null)
                            {
                                control.MessageContainerBorder.Height = (double)args.NewValue;
                            }
                        })));

        /// <summary>
        /// Gets or sets the MessagesListHeight for SMSControl.
        /// </summary>
        /// <value>The height of the messages list.</value>
        public double MessagesListHeight
        {
            get { return (double)GetValue(MessagesListHeightProperty); }
            set { SetValue(MessagesListHeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for MessagesListHeight for SMSControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty MessagesListHeightProperty =
            DependencyProperty.Register("MessagesListHeight", typeof(double), typeof(SMSControl),
            new PropertyMetadata(70.0D,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            SMSControl control = (SMSControl)obj;
                            if (control.ListReceivedMessages != null)
                            {
                                control.ListReceivedMessages.Height = (double)args.NewValue;
                            }
                        })));

        /// <summary>
        /// Gets or sets the phone number text font family.
        /// </summary>
        /// <value>The phone number text font family.</value>
        public FontFamily PhoneNumberTextFontFamily
        {
            get { return (FontFamily)GetValue(PhoneNumberTextFontFamilyProperty); }
            set { SetValue(PhoneNumberTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextFontFamilyProperty =
            DependencyProperty.Register("PhoneNumberTextFontFamily", typeof(FontFamily), typeof(SMSControl),
            new PropertyMetadata(new FontFamily("Georgia")));

        /// <summary>
        /// Gets or sets the size of the phone number text font.
        /// </summary>
        /// <value>The size of the phone number text font.</value>
        public double PhoneNumberTextFontSize
        {
            get { return (double)GetValue(PhoneNumberTextFontSizeProperty); }
            set { SetValue(PhoneNumberTextFontSizeProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextFontSizeProperty =
            DependencyProperty.Register("PhoneNumberTextFontSize", typeof(double), typeof(SMSControl),
            new PropertyMetadata(21.0D));

        /// <summary>
        /// Gets or sets the phone number text foreground.
        /// </summary>
        /// <value>The phone number text foreground.</value>
        public Brush PhoneNumberTextForeground
        {
            get { return (Brush)GetValue(PhoneNumberTextForegroundProperty); }
            set { SetValue(PhoneNumberTextForegroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextForegroundProperty =
            DependencyProperty.Register("PhoneNumberTextForeground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0x33, 0x66, 0x99))));

        /// <summary>
        /// Gets or sets the phone number text background.
        /// </summary>
        /// <value>The phone number text background.</value>
        public Brush PhoneNumberTextBackground
        {
            get { return (Brush)GetValue(PhoneNumberTextBackgroundProperty); }
            set { SetValue(PhoneNumberTextBackgroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextBackgroundProperty =
            DependencyProperty.Register("PhoneNumberTextBackground", typeof(Brush), typeof(SMSControl),
             new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF))));

        /// <summary>
        /// Gets or sets the country text font family.
        /// </summary>
        /// <value>The country text font family.</value>
        public FontFamily CountryTextFontFamily
        {
            get { return (FontFamily)GetValue(CountryTextFontFamilyProperty); }
            set { SetValue(CountryTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextFontFamilyProperty =
            DependencyProperty.Register("CountryTextFontFamily", typeof(FontFamily), typeof(SMSControl),
            new PropertyMetadata(new FontFamily("Arial")));

        /// <summary>
        /// Gets or sets the size of the country text font.
        /// </summary>
        /// <value>The size of the country text font.</value>
        public double CountryTextFontSize
        {
            get { return (double)GetValue(CountryTextFontSizeProperty); }
            set { SetValue(CountryTextFontSizeProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextFontSize.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextFontSizeProperty =
            DependencyProperty.Register("CountryTextFontSize", typeof(double), typeof(SMSControl),
            new PropertyMetadata(12.0D));

        /// <summary>
        /// Gets or sets the country text foreground.
        /// </summary>
        /// <value>The country text foreground.</value>
        public Brush CountryTextForeground
        {
            get { return (Brush)GetValue(CountryTextForegroundProperty); }
            set { SetValue(CountryTextForegroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextForeground.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextForegroundProperty =
            DependencyProperty.Register("CountryTextForeground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00))));

        /// <summary>
        /// Gets or sets the country dropdown background.
        /// </summary>
        /// <value>The country dropdown background.</value>
        public Brush CountryDropdownBackground
        {
            get { return (Brush)GetValue(CountryDropdownBackgroundProperty); }
            set { SetValue(CountryDropdownBackgroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryDropdownBackground.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryDropdownBackgroundProperty =
            DependencyProperty.Register("CountryDropdownBackground", typeof(Brush), typeof(SMSControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF))));

        /// <summary>
        /// Gets or sets the height of the phone number control.
        /// </summary>
        /// <value>The height of the phone number control.</value>
        public double PhoneNumberControlHeight
        {
            get { return (double)GetValue(PhoneNumberControlHeightProperty); }
            set { SetValue(PhoneNumberControlHeightProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberControlHeight.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberControlHeightProperty =
            DependencyProperty.Register("PhoneNumberControlHeight", typeof(double), typeof(SMSControl),
            new PropertyMetadata(40.0D));

        /// <summary>
        /// Gets or sets the maximum length of the message.
        /// </summary>
        /// <value>The maximum length of the message.</value>
        public int MaxMessageLength
        {
            get { return (int)GetValue(MaxMessageLengthProperty); }
            set { SetValue(MaxMessageLengthProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for MaxMessageLength.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty MaxMessageLengthProperty =
            DependencyProperty.Register("MaxMessageLength", typeof(int), typeof(SMSControl),
            new PropertyMetadata(160,
            new PropertyChangedCallback((obj, args) =>
            {
                SMSControl control = (SMSControl)obj;
                if (control.MessageTextbox != null)
                {
                    control.MessageTextbox.MaxLength = (int)args.NewValue;
                }
            })));

        ///// <summary>
        ///// Gets or sets the FileAttachmentListHeight for SMSControl.
        ///// </summary>
        ///// <value>The FileAttachmentListHeight.</value>
        //public double FileAttachmentListHeight
        //{
        //    get { return (double)GetValue(FileAttachmentListHeightProperty); }
        //    set { SetValue(FileAttachmentListHeightProperty, value); }
        //}

        ///// <summary>
        ///// This DependencyProperty acts as the backing store for FileAttachmentListHeight for SMSControl.  This enables animation, styling, binding, etc.
        ///// </summary>
        //public static readonly DependencyProperty FileAttachmentListHeightProperty =
        //    DependencyProperty.Register("FileAttachmentListHeight", typeof(double), typeof(SMSControl),
        //    new PropertyMetadata(70.0D,
        //                new PropertyChangedCallback((obj, args) =>
        //                {
        //                    SMSControl control = (SMSControl)obj;
        //                    if (control.ListFileAttachments != null)
        //                    {
        //                        control.ListFileAttachments.Height = (double)args.NewValue;
        //                    }
        //                })));
        #endregion

        #region event handlers
        private void MessageTextbox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (MessageTextbox.Text.Trim() == DefaultMessageText)
            {
                MessageTextbox.Text = string.Empty;
            }
            MessageTextbox.Opacity = 1;
            ControlHelper.HideAllPopups();
        }

        private void MessageTextbox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (MessageTextbox.Text.Trim() == string.Empty)
            {
                MessageTextbox.Opacity = 0.75;
                MessageTextbox.Text = DefaultMessageText;
            }
        }

        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            //FilesAttachedPanel.Visibility = Visibility.Collapsed;
            if (SendSMSTriggerEvent != null)
            {
                SendSMSTriggerEvent(sender, e);
            }
        }

        //private void AttachFilesImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    if (!IgnoreDefaultFileOpen)
        //    {
        //        OpenFileDialog dlg = new OpenFileDialog();
        //        dlg.Multiselect = AllowMultipleFilesInMMS;
        //        dlg.Filter = FileFilters;

        //        if (dlg.ShowDialog().Value)
        //        {
        //            foreach (var file in dlg.Files)
        //            {
        //                if (!FileAlreadyAdded(file.Name))
        //                {
        //                    Files.Add(file);
        //                }
        //            }
        //        }
        //    }

        //    if (FilesSelectedEvent != null)
        //    {
        //        FilesSelectedEvent(sender, e);
        //    }
        //}
        #endregion

        #region public methods
        /// <summary>
        /// Updates the states.
        /// </summary>
        /// <param name="useTransitions">if set to <c>true</c> [use transitions].</param>
        /// <param name="state">The state.</param>
        /// <param name="messageDeliveryStatus">The message delivery status.</param>
        public void UpdateStates(bool useTransitions, MessageState state = MessageState.None, string messageDeliveryStatus = null)
        {
            switch (state)
            {
                case MessageState.Sending:
                    break;
                case MessageState.SentSuccessfully:
                    MessageDeliveryStatusTextblock.Text = string.IsNullOrEmpty(messageDeliveryStatus) ?
                        (Mode == SMSControlMode.SMS ? ControlResources.SMS_Send_Success : ControlResources.MMS_Send_Success) : messageDeliveryStatus;
                    VisualStateManager.GoToState(this, "Success", true);
                    break;
                case MessageState.SendingFailed:
                    MessageDeliveryStatusTextblock.Text = string.IsNullOrEmpty(messageDeliveryStatus) ?
                        (Mode == SMSControlMode.SMS ? ControlResources.SMS_Send_Failure : ControlResources.MMS_Send_Failure) : messageDeliveryStatus;
                    VisualStateManager.GoToState(this, "Failure", true);
                    break;
                default: //state none
                    break;
            }
        }
        #endregion

        #region private helper methods
        private void SetMessageListVisibility()
        {
            if (receivedMessages.Count > 0)
            {
                ListReceivedMessages.Visibility = Visibility.Visible;
            }
            else
            {
                ListReceivedMessages.Visibility = Visibility.Collapsed;
            }
        }

        //private bool FileAlreadyAdded(string fileName)
        //{
        //    foreach (var file in Files)
        //    {
        //        if (file.Name == fileName) 
        //            return true;
        //    }
        //    return false;
        //}
        #endregion
    }
}
